﻿using System;
using System.Collections.Generic;
using System.Text;

namespace QT.Net
{
    /// <summary>
    /// IP处理方法
    /// </summary>
    public class IP
    {
        /// <summary>
        /// IP地址转换，string转换为long
        /// </summary>
        /// <param name="ip_String">需要转换的string类型IP</param>
        /// <returns></returns>
        public static long IPToLong(string ip_String)
        {
            char[] separator = new char[] { '.' };
            string[] strArray = ip_String.Split(separator);
            long num2 = ((long.Parse(strArray[0]) * 0x100L) * 0x100L) * 0x100L;
            long num3 = (long.Parse(strArray[1]) * 0x100L) * 0x100L;
            long num4 = long.Parse(strArray[2]) * 0x100L;
            long num5 = long.Parse(strArray[3]);
            return (((num2 + num3) + num4) + num5);
        }

        /// <summary>
        /// IP地址转换，long转换为string
        /// </summary>
        /// <param name="ip_Int">需要转换的long类型IP</param>
        /// <returns></returns>
        public static string LongToIP(long ip_Int)
        {
            long num = (long)((ip_Int & 0xff000000L) >> 0x18);
            if (num < 0L)
            {
                num += 0x100L;
            }
            long num2 = (ip_Int & 0xff0000L) >> 0x10;
            if (num2 < 0L)
            {
                num2 += 0x100L;
            }
            long num3 = (ip_Int & 0xff00L) >> 8;
            if (num3 < 0L)
            {
                num3 += 0x100L;
            }
            long num4 = ip_Int & 0xffL;
            if (num4 < 0L)
            {
                num4 += 0x100L;
            }
            return (num.ToString() + "." + num2.ToString() + "." + num3.ToString() + "." + num4.ToString());
        }

        /// <summary>
        /// IP地址转换，string转换为Int64. 格式：192168000001
        /// </summary>
        /// <param name="ip_String">需要转换的string类型IP</param>
        /// <returns></returns>
        public static Int64 IPToInt64(string ip_String)
        {
            string[] IPs = ip_String.Split('.');

            return Convert.ToInt64(IPs[0].PadLeft(3, '0') + IPs[1].PadLeft(3, '0') + IPs[2].PadLeft(3, '0') + IPs[3].PadLeft(3, '0'));
        }

        /// <summary>
        /// 检查IP是否私有地址
        /// </summary>
        /// <param name="ip_Int">待判断的long类型ip</param>
        /// <returns></returns>
        public static bool IsPrivate(long ip_Int)
        {
            if (ip_Int >= IPToLong("10.0.0.0") && ip_Int <= IPToLong("10.255.255.255")) return true;
            if (ip_Int >= IPToLong("127.0.0.0") && ip_Int <= IPToLong("127.255.255.255")) return true;
            if (ip_Int >= IPToLong("172.16.0.0") && ip_Int <= IPToLong("172.31.255.255")) return true;
            if (ip_Int >= IPToLong("168.254.0.0") && ip_Int <= IPToLong("168.254.255.255")) return true;
            if (ip_Int >= IPToLong("192.168.0.0") && ip_Int <= IPToLong("192.168.255.255")) return true;

            return false;
        }

        /// <summary>
        /// 检查IP是否私有地址
        /// </summary>
        /// <param name="ip_String">待判断的string类型ip</param>
        /// <returns></returns>
        public static bool IsPrivate(string ip_String)
        {
            long ip_Int = IPToLong(ip_String);
            return IsPrivate(ip_Int);
        }

        /// <summary>
        /// 判断IP类型
        /// </summary>
        /// <param name="ip_Int">待判断的int类型IP</param>
        /// <returns></returns>
        public static string IPType(long ip_Int)
        {
            if (ip_Int >= IPToLong("1.0.0.0") && ip_Int <= IPToLong("126.255.255.255")) return "A";
            if (ip_Int >= IPToLong("127.0.0.0") && ip_Int <= IPToLong("127.255.255.255")) return "LOCAL";
            if (ip_Int >= IPToLong("168.254.0.0") && ip_Int <= IPToLong("168.254.255.255")) return "DHCP";
            if (ip_Int >= IPToLong("128.16.0.0") && ip_Int <= IPToLong("191.255.255.255")) return "B";
            if (ip_Int >= IPToLong("192.0.0.0") && ip_Int <= IPToLong("223.255.255.255")) return "C";
            if (ip_Int >= IPToLong("224.0.0.0") && ip_Int <= IPToLong("239.255.255.255")) return "D";
            if (ip_Int >= IPToLong("240.0.0.0") && ip_Int <= IPToLong("255.255.255.255")) return "E";
            return "IANA";
        }

        /// <summary>
        /// 判断IP类型
        /// </summary>
        /// <param name="ip_String">待判断的string类型IP</param>
        /// <returns></returns>
        public static string IPType(string ip_String)
        {
            long ip_Int = IPToLong(ip_String);
            return IPType(ip_Int);
        }

        /// <summary>
        /// 获取给定IP的默认子网掩码
        /// </summary>
        /// <param name="ip_Int">数字格式的IP地址</param>
        /// <returns>string</returns>
        public static string GetdefaultMask(long ip_Int)
        {
            if (ip_Int >= IPToLong("0.0.0.1") && ip_Int <= IPToLong("127.255.255.255")) return "255.0.0.0";
            if (ip_Int >= IPToLong("128.0.0.0") && ip_Int <= IPToLong("191.255.255.255")) return "255.255.0.0";
            if (ip_Int >= IPToLong("192.0.0.0") && ip_Int <= IPToLong("223.255.255.255")) return "255.255.255.0";
            return "255.255.255.255";
        }

        /// <summary>
        /// 获取给定IP的默认子网掩码
        /// </summary>
        /// <param name="ip_String">字符串格式的IP地址</param>
        /// <returns>string</returns>
        public static string GetdefaultMask(string ip_String)
        {
            long ip_Int = IPToLong(ip_String);
            return GetdefaultMask(ip_Int);
        }

        /// <summary>
        /// 对比两个IP，返回ip1与ip2的差值
        /// </summary>
        /// <param name="ip1">ip1</param>
        /// <param name="ip2">ip2</param>
        /// <returns>long</returns>
        public static long Compare(string ip1, string ip2)
        {
            return IPToLong(ip1) - IPToLong(ip2);
        }
    }
}
